#coding=utf8

from xpinyin import Pinyin
import json
import re

poems = json.load(open('poems.json','r'))
p = Pinyin()

for poem in poems:
  content = poem['content']
  print(content)

  shiju = re.sub(r'[，。？！]', '##', content).strip().split('##')
  shiju = list(filter(lambda x: len(x) > 0, shiju))
  print(shiju)

  pinyin = []
  for sj in shiju:
    pinyin.append(p.get_pinyin(sj).split('-'))
  print(pinyin)
  
  pinyin2 = []
  for sj in shiju:
    pinyin2.append(p.get_pinyin(sj, tone_marks='marks').split('-'))
  print(pinyin2)

  shiju = [[y for y in x] for x in shiju]
  print(shiju)

  poem['shiju'] = shiju
  poem['pinyin'] = pinyin
  poem['pinyin2'] = pinyin2

json.dump(poems, open('唐诗宋词-拼音.json','w'), ensure_ascii=False, indent=2)
